import dawdreamer


class SimpleDAW:
    def __init__(self, plugin, sample_rate=44100, bpm=120):
        self.engine = dawdreamer.RenderEngine(sample_rate, 256)
        self.instr = self.engine.make_plugin_processor('instr_1', plugin)
        self.engine.load_graph([(self.instr, [])])
        self.engine.set_bpm(bpm)

    def load_midi(self, midi_filename=''):
        if midi_filename:
            self.instr.load_midi(midi_filename)

    def load_preset(self, preset_filename=''):
        if preset_filename:
            self.instr.load_preset(preset_filename)

    def set_params(self, params):
        for index, param in enumerate(params):
            self.instr.set_parameter(index, param)

    def get_params(self, preset_filename='', return_names=False):
        if preset_filename:
            self.instr.load_preset(preset_filename)
        if return_names:
            return tuple((self.instr.get_parameter_name(i), self.instr.get_parameter(i))
                         for i in range(self.instr.get_plugin_parameter_size()))
        else:
            return tuple(self.instr.get_parameter(i) for i in range(self.instr.get_plugin_parameter_size()))

    def count_params(self, preset_filename=''):
        if preset_filename:
            self.instr.load_preset(preset_filename)
        return self.instr.get_plugin_parameter_size()

    def render(self, seconds):
        self.engine.render(seconds)
        return self.engine.get_audio().transpose((1, 0))

# daw = SimpleDAW(plugin='C:/VstPlugins/64bit/Sylenth1.dll')
# daw.render(2.)
